/**
 * 本代码实现了主界面功能
 */
package com.bf.zk;

import java.awt.Color;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsConfiguration;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import org.eclipse.swt.graphics.RGB;

import com.bf.zk.constants.Constants;
import com.bf.zk.node.NodeManage;
import com.bf.zk.node.OpenBrowse;
import com.bf.zk.user.SuperUserManage;
import com.sun.org.apache.xml.internal.security.algorithms.implementations.IntegrityHmac;

/**
 * 程序的主要功能界面
 */
public class MainWindow extends JFrame implements ActionListener, MouseListener {

	private static final long serialVersionUID = 1L;
	public JFrame MainWindow;
	public JButton userManage;
	public JButton noteManage;
	public JButton fileManage;
	public JButton taskManage;
	public JButton logManage;
	public JButton luboManage;
	public JButton logOut;
	ImageIcon iconc;
	ImageIcon iconc1;
	private Color mycColor;
	@SuppressWarnings("unused")
	private JFrame parent;
	private ImageIcon background;
	private JPanel imagePanel;
	
	private JLabel titleLabel;
	private String titleString = "";
	private Integer titleWidth = 0;
	private Integer titleCharStarX = 0;
	private Integer titleFrontSize = 60;

	public MainWindow(JFrame parent, boolean modal) {
		this.parent = parent;
		// 设置显示样式
		JFrame.setDefaultLookAndFeelDecorated(true);
		 Dimension   screensize   =   Toolkit.getDefaultToolkit().getScreenSize();
		 int width = (int)screensize.getWidth();
	     int height = (int)screensize.getHeight();
	     int x=(width-1200)/5;
	     int y=height/2-150;
	     int wangh=300;
	     
	     int z0=680;
	     int x0=(width-z0)/2;
	     int y0=height/2-300;
		// 初始化界面以及按钮、输入框等
	    GraphicsConfiguration gc = getGraphicsConfiguration();
		MainWindow = new JFrame( gc);
		MainWindow.setTitle("ZNZK");
		MainWindow.setSize(width, height-65);
		MainWindow.setDefaultCloseOperation(3);
		MainWindow.setResizable(false);

		// 居中显示
		MainWindow.setLayout(null);
		MainWindow.setLocationRelativeTo(parent);
		MainWindow.setIconImage(Toolkit.getDefaultToolkit().createImage("./pic/Log.jpg"));

		MainWindow.getContentPane().setBackground(Constants.mycColor);
		MainWindow.setLocation(0, 0);
		
		//标题
				//获取配置文件信息
				Properties props = new Properties();
		        try {
		            FileInputStream in = null;
		            in = new FileInputStream("ui.properties");
		            props.load(in);
		            titleString = props.getProperty("title");
		        } catch (IOException e) {
		            System.err.println(e);
		        }
				//35号字，1个字35
				titleWidth = titleString.length() * titleFrontSize;
				titleCharStarX = (int) ((width/2) - (titleFrontSize/2) * titleString.length());
				titleLabel = new JLabel(titleString);
				titleLabel.setBounds(titleCharStarX, 62, titleWidth, titleFrontSize);
				titleLabel.setFont(new Font("微软雅黑", Font.BOLD, titleFrontSize));
				titleLabel.setForeground(new Color(137,137,137));
		
		
		// 设置背景图片
//		background = new ImageIcon("./pic/bg.png");// 背景图片
//		JLabel label = new JLabel(background);
		// 把标签的大小位置设置为图片刚好填充整个面板
//		label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
//		MainWindow.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
		MainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		// 菜单栏
//		JMenuBar mb = new JMenuBar();
//		mb.setBounds(0, 0, 800, 30);
//		setJMenuBar(mb);
//		// 菜单
//		JMenu m1 = new JMenu("File ");
//		JMenu m3 = new JMenu("Help");
//		mb.add(m1);
//		mb.add(m3);
//		// 菜单项
//		JMenuItem mi1 = new JMenuItem("北方官网");
//		JMenuItem mi3 = new JMenuItem("退出");
//		m1.add(mi1);
//		m1.addSeparator();
//		m1.add(mi3);
//		mb.setOpaque(false);
//		JMenuItem m2 = new JMenuItem("帮助");
//		m3.add(m2);
//		// 菜单事件
//		mi1.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				try {
//					Desktop.getDesktop().browse(new URI("http://www.bfhualu.com"));
//				} catch (IOException e1) {
//					e1.printStackTrace();
//				} catch (URISyntaxException e1) {
//					e1.printStackTrace();
//				}
//			}
//		});
//		mi3.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				System.exit(0);
//			}
//		});
//		m2.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				File path = new File(".\\Help\\LGHelp.pdf");
//				try {
//					Runtime.getRuntime().exec("rundll32 url.dll FileProtocolHandler " + path.getCanonicalPath());
//				} catch (IOException e1) {
//					e1.printStackTrace();
//				}
//			}
//		});
		// 用户管理
		userManage = new JButton();
		userManage.setIcon(new ImageIcon("./pic/userm.png"));
		userManage.setPressedIcon(new ImageIcon("./pic/userm0.png"));
		userManage.setContentAreaFilled(false);
		userManage.setBorderPainted(false);
		userManage.setFocusPainted(false);
		userManage.setActionCommand("userManage");
		userManage.setBounds(20, height-210, 200, 120);
//		userManage.setFont(new Font("微软雅黑", Font.BOLD, 24));
//		userManage.setForeground(Color.BLACK);
//		userManage.setBackground(mycColor);

		// 节点管理
		noteManage = new JButton();
		noteManage.setIcon(new ImageIcon("./pic/nodem.png"));
		noteManage.setPressedIcon(new ImageIcon("./pic/nodem0.png"));
		noteManage.setContentAreaFilled(false);
		noteManage.setBorderPainted(false);
		noteManage.setFocusPainted(false);
		noteManage.setActionCommand("noteManage");
		noteManage.setBounds(240, height-210, 200, 120);
//		noteManage.setFont(new Font("黑体", Font.BOLD, 24));
//		noteManage.setForeground(Color.BLACK);
//		noteManage.setBackground(mycColor);

		// U型幕展项
		fileManage = new JButton();
		fileManage.setIcon(new ImageIcon("./pic/uxm.png"));
		fileManage.setPressedIcon(new ImageIcon("./pic/uxm0.png"));
		fileManage.setContentAreaFilled(false);
		fileManage.setBorderPainted(false);
		fileManage.setFocusPainted(false);
		fileManage.setActionCommand("fileManage");
		fileManage.setBounds(x, y, wangh, wangh);//30
//		fileManage.setFont(new Font("微软雅黑", Font.BOLD, 24));
//		fileManage.setForeground(Color.BLACK);
//		fileManage.setBackground(mycColor);

		// 沙盘展项
		taskManage = new JButton();
		taskManage.setIcon(new ImageIcon("./pic/sp.png"));
		taskManage.setPressedIcon(new ImageIcon("./pic/sp0.png"));
		taskManage.setContentAreaFilled(false);
		taskManage.setBorderPainted(false);
		taskManage.setFocusPainted(false);
		taskManage.setActionCommand("taskManage");
		taskManage.setBounds(2*x+300, y, wangh, wangh);//220
//		taskManage.setFont(new Font("微软雅黑", Font.BOLD, 24));
//		taskManage.setForeground(Color.BLACK);
//		taskManage.setBackground(mycColor);

		// 环幕展项
		logManage = new JButton();
		logManage.setIcon(new ImageIcon("./pic/hm.png"));
		logManage.setPressedIcon(new ImageIcon("./pic/hm0.png"));
		logManage.setContentAreaFilled(false);
		logManage.setBorderPainted(false);
		logManage.setFocusPainted(false);
		logManage.setActionCommand("logManage");
		logManage.setBounds(3*x+600, y, wangh, wangh);//420
//		logManage.setFont(new Font("微软雅黑", Font.BOLD, 24));
//		logManage.setForeground(Color.BLACK);
//		logManage.setBackground(mycColor);

		//其他展项
		luboManage = new JButton();
		luboManage.setIcon(new ImageIcon("./pic/qt.png"));
		luboManage.setPressedIcon(new ImageIcon("./pic/qt0.png"));
		luboManage.setContentAreaFilled(false);
		luboManage.setBorderPainted(false);
		luboManage.setFocusPainted(false);
		luboManage.setActionCommand("luboManage");
		luboManage.setBounds(4*x+900, y, wangh, wangh);//620
//		luboManage.setFont(new Font("微软雅黑", Font.BOLD, 24));
//		luboManage.setForeground(Color.BLACK);
//		luboManage.setBackground(mycColor);

		// 退出系统
		logOut = new JButton();
		logOut.setIcon(new ImageIcon("./pic/logout.png"));
		logOut.setPressedIcon(new ImageIcon("./pic/logout0.png"));
		logOut.setContentAreaFilled(false);
		logOut.setBorderPainted(false);
		logOut.setFocusPainted(false);
		logOut.setActionCommand("logOut");
		logOut.setBounds(width-220, height-210, 200, 120);
		logOut.setFont(new Font("微软雅黑", Font.BOLD, 20));
		logOut.setForeground(Color.BLACK);
		logOut.setBackground(mycColor);

//		JLabel nameJLabel = new JLabel("中宁展示中心控制系统 ");//
//		nameJLabel.setBounds(x0, y0, z0, 100);
//		nameJLabel.setFont(new Font("微软雅黑", Font.BOLD, 60));
//		JLabel nameJLabel0 = new JLabel("");// 后台管理软件V1.0
//		nameJLabel0.setBounds(310, 150, 790, 100);
//		nameJLabel0.setFont(new Font("微软雅黑", 30, 30));

		//MainWindow.add(mb);
		MainWindow.add(titleLabel);
		MainWindow.add(userManage);
		MainWindow.add(noteManage);
		MainWindow.add(fileManage);
		MainWindow.add(taskManage);
		MainWindow.add(logManage);
		MainWindow.add(luboManage);

		MainWindow.add(logOut);
		//MainWindow.add(nameJLabel);
//		MainWindow.add(nameJLabel0);
//		MainWindow.add(label);
		
		userManage.addActionListener(this);
		noteManage.addActionListener(this);
		fileManage.addActionListener(this);
		taskManage.addActionListener(this);
		logManage.addActionListener(this);
		luboManage.addActionListener(this);
		logOut.addActionListener(this);

		userManage.addMouseListener(this);
		noteManage.addMouseListener(this);
		fileManage.addMouseListener(this);
		taskManage.addMouseListener(this);
		logManage.addMouseListener(this);
		luboManage.addMouseListener(this);
		logOut.addMouseListener(this);
		Constants.LGmin = MainWindow;
		MainWindow.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if ((e.getActionCommand().equals("userManage"))&&(Constants.LoginName.equals("admin"))) {
			try {
				new SuperUserManage(MainWindow, rootPaneCheckingEnabled);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			MainWindow.setVisible(false);
		}

		if (e.getActionCommand().equals("noteManage")) {
			try {
				new NodeManage(MainWindow, rootPaneCheckingEnabled);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			MainWindow.setVisible(false);
		}

		if (e.getActionCommand().equals("fileManage")) {
			String pw = null;
			try {
				String fileName = "./sqlite/znzk.db";
				Class.forName("org.sqlite.JDBC");
				Connection conn = DriverManager.getConnection("jdbc:sqlite:" + fileName);
				Statement stmt = conn.createStatement();
				ResultSet rs = stmt.executeQuery("SELECT nodeip FROM znzk_node where nodename='中宁枸杞';");
				while (rs.next()) {
					pw = rs.getString("nodeip"); // Column 3
					Constants.ZKURL = "http://" + pw + "/PC.html";
					Constants.ZKName = "中宁枸杞";
				}
				conn.close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			MainWindow.setVisible(false);
			new com.bf.zk.node.ZkNode(MainWindow, rootPaneCheckingEnabled);
		}
		if (e.getActionCommand().equals("taskManage")) {
			String pw = null;
			try {
				String fileName = "./sqlite/znzk.db";
				Class.forName("org.sqlite.JDBC");
				Connection conn = DriverManager.getConnection("jdbc:sqlite:" + fileName);
				Statement stmt = conn.createStatement();
				ResultSet rs = stmt.executeQuery("SELECT nodeip FROM znzk_node where nodename='城市沙盘';");
				while (rs.next()) {
					pw = rs.getString("nodeip"); // Column 3
					Constants.ZKURL = "http://" + pw + "/PC.html";
					Constants.ZKName = "城市沙盘";
				}
				conn.close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			MainWindow.setVisible(false);
			new com.bf.zk.node.ZkNode(MainWindow, rootPaneCheckingEnabled);
		}
		if (e.getActionCommand().equals("logManage")) {
			String pw = null;
			try {
				String fileName = "./sqlite/znzk.db";
				Class.forName("org.sqlite.JDBC");
				Connection conn = DriverManager.getConnection("jdbc:sqlite:" + fileName);
				Statement stmt = conn.createStatement();
				ResultSet rs = stmt.executeQuery("SELECT nodeip FROM znzk_node where nodename='生态中宁';");
				while (rs.next()) {
					pw = rs.getString("nodeip"); // Column 3
					Constants.ZKURL = "http://" + pw + "/PC.html";
					Constants.ZKName = "生态中宁";
				}
				conn.close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			MainWindow.setVisible(false);
			new com.bf.zk.node.ZkNode(MainWindow, rootPaneCheckingEnabled);
		}

		if (e.getActionCommand().equals("luboManage")) {
			String pw = null;
			try {
				String fileName = "./sqlite/znzk.db";
				Class.forName("org.sqlite.JDBC");
				Connection conn = DriverManager.getConnection("jdbc:sqlite:" + fileName);
				Statement stmt = conn.createStatement();
				ResultSet rs = stmt.executeQuery("SELECT nodeip FROM znzk_node where nodename='其他展项';");
				while (rs.next()) {
					pw = rs.getString("nodeip"); // Column 3
					Constants.ZKURL = "http://" + pw + "/PC.html";
					Constants.ZKName = "其他展项";
				}
				conn.close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			MainWindow.setVisible(false);
			new com.bf.zk.node.ZkNode(MainWindow, rootPaneCheckingEnabled);
			//OpenBrowse.openURL(Constants.ZKURL);
		}

		// 退出系统
		if (e.getActionCommand().equals("logOut")) {
			System.exit(0);
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {}

	@Override
	public void mousePressed(MouseEvent e) {}

	@Override
	public void mouseReleased(MouseEvent e) {}

	@Override
	public void mouseEntered(MouseEvent e) {
		if (e.getSource() == userManage) {
			//userManage.setBackground(mycColor);
			userManage.setIcon(new ImageIcon("./pic/userm0.png"));
		} else if (e.getSource() == noteManage) {
			//noteManage.setBackground(mycColor);
			noteManage.setIcon(new ImageIcon("./pic/nodem0.png"));
		} else if (e.getSource() == fileManage) {
			//fileManage.setBackground(mycColor);
			fileManage.setIcon(new ImageIcon("./pic/uxm0.png"));
		} else if (e.getSource() == taskManage) {
			//taskManage.setBackground(mycColor);
			taskManage.setIcon(new ImageIcon("./pic/sp0.png"));
		} else if (e.getSource() == logManage) {
//			logManage.setBackground(mycColor);
			logManage.setIcon(new ImageIcon("./pic/hm0.png"));
		} else if (e.getSource() == luboManage) {
//			luboManage.setBackground(mycColor);
			luboManage.setIcon(new ImageIcon("./pic/qt0.png"));
		} else if (e.getSource() == logOut) {
			//logOut.setBackground(mycColor);
			logOut.setIcon(new ImageIcon("./pic/logout0.png"));
		}

		// MainWindow.add(logOut);
		

	}

	@Override
	public void mouseExited(MouseEvent e) {
		if (e.getSource() == userManage) {
			//userManage.setBackground(Color.white);
			userManage.setIcon(new ImageIcon("./pic/userm.png"));
		} else if (e.getSource() == noteManage) {
			//noteManage.setBackground(Color.white);
			noteManage.setIcon(new ImageIcon("./pic/nodem.png"));
		} else if (e.getSource() == fileManage) {
			//fileManage.setBackground(Color.white);
			fileManage.setIcon(new ImageIcon("./pic/uxm.png"));
		} else if (e.getSource() == taskManage) {
			//taskManage.setBackground(Color.white);
			taskManage.setIcon(new ImageIcon("./pic/sp.png"));
		} else if (e.getSource() == logManage) {
//			logManage.setBackground(Color.white);
			logManage.setIcon(new ImageIcon("./pic/hm.png"));
		} else if (e.getSource() == luboManage) {
//			luboManage.setBackground(Color.white);
			luboManage.setIcon(new ImageIcon("./pic/qt.png"));
		} else if (e.getSource() == logOut) {
			//logOut.setBackground(Color.white);
			logOut.setIcon(new ImageIcon("./pic/logout.png"));
		}
	}

}
